<template>
  <div id="sales">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="商品名称" label="宝贝" width="150">
      </el-table-column>
      <el-table-column prop="价格" label="金额" width="150">
      </el-table-column>
      <el-table-column prop="下单时间" label="下单时间" width="150">
      </el-table-column>
      <el-table-column prop="物流名称" label="物流名称" width="150">
      </el-table-column>
      <el-table-column prop="物流单号" label="物流单号" width="150"></el-table-column>
      <el-table-column prop="发货时间" label="发货时间" width="150"></el-table-column>
      <el-table-column v-if="is_show" label="操作">
        <template slot-scope="scope">
          <el-button type="danger" v-on:click="cclick(scope.row.id)" >{{i_state}}</el-button>
        </template>
      </el-table-column>
      <el-table-column v-if="cancel_show" label="操作">
        <template slot-scope="scope2">
          <el-button  type="danger" v-on:click="ccancel(scope2.row.id)" >取消订单</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>


</template>

<script>
  import qs from 'qs';
  import axios from 'axios'
const BASE_URL = 'http://127.0.0.1:8886';

  export default {
    data() {
      return {
        tableData: [],
        state: 0,
        fits: [],
        i_state:'',
        is_show:false,
        cancel_show:false
      }
    },
    watch:{
      "$route":"flush"
    },
    mounted(){

      this.getdata();
    }
    ,
    methods: {
      is_confirm(id) { //下单模态框
        this.$confirm('是否进行确认收货?', '提示', {
          confirmButtonText: '是的',
          cancelButtonText: '否',
          type: 'warning'
        }).then(() => {
          this.confirm_order(id);
        }).catch(() => {
          //this.$router.back();
        });
      },
      is_pay(id) { //下单模态框
        this.$confirm('是否进行付款?', '提示', {
          confirmButtonText: '立即付款',
          cancelButtonText: '稍后付款',
          type: 'warning'
        }).then(() => {
          this.pay_order(id);
        }).catch(() => {
          //this.$router.back();
        });
      },
      cry_evaluation_order(id) {
        this.$prompt('请输入评价内容', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputErrorMessage: '输入不能为空',
          inputValidator: (value) => {       // 点击按钮时，对文本框里面的值进行验证
            if(!value) {
              return '输入不能为空';
            }
          }
        }).then(({ value }) => {
          this.evaluation_order(id,value);
          console.log("dddd"+value);
        }).catch(() => {

        });
      },
      cancel_ok() { //取消模态框
        this.$alert('取消成功', '提示', {
          confirmButtonText: '确定',
          callback: action => {

          } });
      },
      pay_ok() { //取消模态框
        this.$alert('付款成功', '提示', {
          confirmButtonText: '确定',
          callback: action => {

          } });
      },
      confirm_ok() { //取消模态框
        this.$alert('确认收货成功', '提示', {
          confirmButtonText: '确定',
          callback: action => {

          } });
      },
      evaluation_ok() { //取消模态框
        this.$alert('评价成功', '提示', {
          confirmButtonText: '确定',
          callback: action => {

          } });
      },
      flush()
      {
        this.getdata();
      },
      cclick(id)
      {
        let index=this.getpar('state');
        if (index==0) {

          this.is_pay(id);
        }
        else if(index==2)
        {
          this.confirm_order(id);
        }
      else if(index==3)
        {
          this.cry_evaluation_order(id);
        }
        console.log(id)
      },
      ccancel(order_id)//取消订单
      {
        this.ccancel_order(order_id);
        console.log("cancel:"+order_id)
      },
      ccancel_order(order_id){
        axios.post(BASE_URL +'/order/cancel_order',
          qs.stringify({token:window.localStorage.getItem('token'),id:order_id}),
          {'headers': {'Content-Type': 'application/x-www-form-urlencoded'}})
          .then(re =>{
            console.log(re.data.备注);
            if(re.data.状态码 === 200)
            {
              this.cancel_ok();
              this.getdata();
              console.log(re.data.备注);
            }
            else
            {
            }
          })
          .catch((error =>{
            console.log(error)
          }))
      },
      pay_order(order_id){
        axios.post(BASE_URL +'/order/pay_order',
          qs.stringify({token:window.localStorage.getItem('token'),id:order_id}),
          {'headers': {'Content-Type': 'application/x-www-form-urlencoded'}})
          .then(re =>{
            console.log(re.data.备注);
            if(re.data.状态码 === 200)
            {
              this.pay_ok();
              this.getdata();
              console.log(re.data.备注);
            }
            else
            {
            }
          })
          .catch((error =>{
            console.log(error)
          }))
      },
      confirm_order(order_id){
        axios.post(BASE_URL +'/order/confirm_order',
          qs.stringify({token:window.localStorage.getItem('token'),id:order_id}),
          {'headers': {'Content-Type': 'application/x-www-form-urlencoded'}})
          .then(re =>{
            console.log(re.data.备注);
            if(re.data.状态码 === 200)
            {
              this.confirm_ok();
              this.getdata();
              console.log(re.data.备注);
            }
            else
            {
            }
          })
          .catch((error =>{
            console.log(error)
          }))
      },
      evaluation_order(order_id,text){
        axios.post(BASE_URL +'/order/evaluation_order',
          qs.stringify({token:window.localStorage.getItem('token'),id:order_id,text:text,image_url:"null",type:0}),
          {'headers': {'Content-Type': 'application/x-www-form-urlencoded'}})
          .then(re =>{
            console.log(re.data.备注);
            if(re.data.状态码 === 200)
            {
              this.evaluation_ok();
              this.getdata();
              console.log(re.data.备注);
            }
            else
            {
            }
          })
          .catch((error =>{
            console.log(error)
          }))
      },
      getdata()
      {
        this.tableData=[];
        let index=this.getpar('state');
        if (index==0){
          this.is_show=true;
          this.cancel_show=true;
          this.i_state='支付';
        } else if(index==1){
          this.is_show=false;
          this.cancel_show=false;
          this.i_state='';
        }else if(index==2){
          this.is_show=true;
          this.cancel_show=false;
          this.i_state='确认收货';
        }else if(index==3){
          this.is_show=true;
          this.cancel_show=false;
          this.i_state='评价';
        }else {
          this.is_show=false;
          this.cancel_show=false;
          this.i_state='';
        }
        console.log("fff"+this.getpar('state'));
        axios.post(BASE_URL +'/order/get_my_order_list_for_state',
          qs.stringify({token:window.localStorage.getItem('token'),state:this.getpar('state')}),
          {'headers': {'Content-Type': 'application/x-www-form-urlencoded'}})
          .then(re =>{
            console.log(re.data.备注);
            if(re.data.状态码 === 200)
            {
              this.tableData= re.data.列表;
              console.log(re.data.备注);
            }
            else
            {
            }
          })
          .catch((error =>{
            console.log(error)
          }))
      },
      go_add_order(item)
      {

        this.$router.push('/add_order#id='+item.商品id);
      },
      getpar(par){
        //获取当前URL
        let local_url = document.location.href;
        //获取要取得的get参数位置
        let get = local_url.indexOf(par + "=");
        if (get == -1) {
          return "";
        }
        //截取字符串
        let get_par = local_url.slice(par.length + get + 1);
        //判断截取后的字符串是否还有其他get参数
        let nextPar = get_par.indexOf("&");
        if (nextPar != -1) {
          get_par = get_par.slice(0, nextPar);

        }
        nextPar = get_par.indexOf("?");
        if (nextPar != -1) {
          get_par = get_par.slice(0, nextPar);
        }
        nextPar = get_par.indexOf("#");
        if (nextPar != -1) {
          get_par = get_par.slice(0, nextPar);
        }
        return decodeURI(get_par);
      },
      get_image(id)
      {
        return BASE_URL+"/images/small/"+id+".jpg";
      }
    },
  }
</script>

<style scoped>

</style>
